//index.js
//获取应用实例
const app = getApp();
const service = require('../../../utils/service.js');
// const wxCloud = require('../../../wxCloud/index.js');

Page({
  data: {
    // 热门路线图标
    ticketIcon: app.globalData.config.ticketIcon,

    animationData: '',
    // 加载图
    showLoading: true,
    // 显示内容
    showContainer: false,
    banner: [],
    // 菜单列表
    Menu: [],
    // 宣传语
    slogan: '',
    // 热门线路
    hotLine: [],

    // 优惠券信息
    couponInfo: {}
  },

  onLoad: function(options) {
    let that = this;
    that.loadRouteHomepageData();
    console.log(options);
    // 是否是通过分享进入
    if (options.isShowCoupon && Number(options.isShowCoupon)) {
      // 优惠券弹窗
      this.loadCouponInfo();
    }
    
    this.couponDialog = this.selectComponent("#coupon-dialog");
  },

  // 获取首页信息
  // loadRouteHomepageData() {
  //   let that = this;
  //   app.utils.createAll([
  //     // 云函数-获取Banner
  //     wxCloud.Cloud.request({
  //       'action': 'getBanner',
  //       'params': {
  //         'filter': {
  //           'status': 1
  //         },
  //         'sort': {
  //           'fieldName': 'weight',
  //           'order': 'desc'
  //         }
  //       }
  //     }),
  //     // 云函数-获取Menu
  //     wxCloud.Cloud.request({
  //       'action': 'getMenu',
  //       'params': {
  //         'filter': {
  //           'status': 1
  //         },
  //         'sort': {
  //           'fieldName': 'weight',
  //           'order': 'desc'
  //         }
  //       }
  //     }),
  //     // 云函数-获取Slogan
  //     wxCloud.Cloud.request({
  //       'action': 'getSlogan'
  //     }),
  //     // 获取热门线路
  //     service.getConnect(service.api.getRouteHomepageData)
  //   ]).then(res => {
  //     console.log('========云函数请求结果========');
  //     console.log(res);
  //     // 获取banner返回值
  //     let banner = res[0]['data']['list'];
  //     if (banner.length === 0) {
  //       banner = that.data.banner;
  //     }
  //     // 获取menu返回值
  //     let menu = res[1]['data']['list'];
  //     if (menu.length === 0) {
  //       menu = that.data.Menu;
  //     }
  //     // 获取slogan返回值
  //     let slogan = res[2]['data']['list']['content'] || that.data.slogan;

  //     // 获取热门路线返回值
  //     let hotLine = res[3].data['hotLine'];

  //     that.setData({
  //       banner: banner,
  //       Menu: menu,
  //       slogan: slogan,
  //       hotLine: hotLine,
  //     })
  //     that.fadeOut();
  //     wx.hideLoading();
  //   })
  // },

  // 获取首页信息
  loadRouteHomepageData() {
    let that = this;
    service.getConnect(service.api.getRouteHomepageData).then(res => {
      // 获取banner返回值
      let banner = res.data['banner'];
      if (banner.length === 0) {
        banner = that.data.banner;
      }
      // 获取menu返回值
      let menu = res.data['Menu'];
      if (menu.length === 0) {
        menu = that.data.Menu;
      }
      // 获取slogan返回值
      let slogan = res.data['settingData']['content'] || that.data.slogan;

      // 获取热门路线返回值
      let hotLine = res.data['hotLine'];

      that.setData({
        banner: banner,
        Menu: menu,
        slogan: slogan,
        hotLine: hotLine,
      })
      that.fadeOut();
      wx.hideLoading();
    })
  },

  // 获取优惠券信息
  loadCouponInfo(){
    let that = this;
    service.getConnect(service.api.getRegisterCouponInfo).then(function (res) {
      if(res.data){
        let couponInfo = res.data;
        if(res.data.condition==='1'){
          couponInfo.condition = '新用户注册';
        } else if (res.data.condition === '2'){
          couponInfo.condition = '单笔订单金额满'+res.data.content+ '元可使用';
        } else if (res.data.condition === '3'){
          couponInfo.condition = '指定用户可用';
        } else {
          couponInfo.condition = '无限制';
        }
        couponInfo.condition = couponInfo.condition + ', 有效期为' + res.data.timeFrom + '至' + res.data.timeTo;
        that.setData({
          couponInfo: couponInfo
        })
        that.couponDialog.showLoginDialog();
      }
    })
  },

  // 领取优惠券
  receiveCoupon() {
    let that = this;
    service.getConnect(service.api.receiveCoupon, {id: this.data.couponInfo['id']}).then(function (res) {
      wx.showToast({
        title: '领取成功~',
      })
      that.couponDialog.hideLoginDialog();
    })
  },

  // 跳转页面
  bindJumpToPage(e) {
    let page = e.currentTarget.dataset.page;
    page.url && app.jumpToPage('/' + page.url, page.title);
  },

  bindToTimetable(e) {
    if (app.globalData.config['homepageHotLine'] === 1) {
      // 巴士路线热门路线班次
      wx.navigateTo({
        url: '/pages/route/daily/timetable/timetable?lineId=' + e.currentTarget.dataset.id + '&chooseTime=' + app.utils.parseTime(new Date(), '{y}-{m}-{d}')
      })
    } else if (app.globalData.config['homepageHotLine'] === 2) {
      // 包车路线热门路线班次
      wx.navigateTo({
        url: '/pages/route/fixed/timetable/timetable?lineId=' + e.currentTarget.dataset.id + '&chooseTime=' + app.utils.parseTime(new Date(), '{y}-{m}-{d}')
      })
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.utils.initWxappStyle(app, this, this.initIcom);
    this.loadRouteHomepageData();
    wx.stopPullDownRefresh();
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    // app.pageOnloadInit(this, app);
    app.utils.initWxappStyle(app, this, this.initIcom);
  },

  initIcom(){
    this.setData({
      ticketIcon: app.globalData.config.ticketIcon
    })
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },

  // 逐渐消失
  fadeOut: function () {
    var that = this;
    var animation = wx.createAnimation({
      delay: 600,
      duration: 400,
      timingFunction: 'ease-in'
    })
    this.animation = animation
    animation.opacity(0).step()
    this.setData({
      animationData: animation.export(),
      showContainer: true
    })
    setTimeout(function(){
      that.setData({
        showLoading: false
      })
    }, 1000);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let wxappStyle = wx.getStorageSync('wxappStyle');
    let sharePath = '/pages/index/index';
    wx.getStorageSync('token') && (sharePath = sharePath + '?token=' + wx.getStorageSync('token'));
    console.log('===================');
    console.log(sharePath);
    return {
      title: app.globalData['wxappStyle']['wxappTitle'] || wxappStyle['wxappTitle'],
      path: sharePath
    }
  }
})